package com.example.admin.testdemo.application;

import android.app.Application;
import android.content.Intent;
import android.util.Log;

import com.example.admin.testdemo.activity.MainActivity;
import com.example.admin.testdemo.constant.Constant;
import com.example.admin.testdemo.helper.LogcatHelper;
import com.example.admin.testdemo.util.FileUtil;
import com.example.admin.testdemo.util.Session;


/**
 * @author Created by apple on 2017/11/22.
 */

public class App extends Application {
    private static String TAG="QQ";
    static App instance;

    public static App getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
        copyFile();
        creatFolder();
//        Thread.setDefaultUncaughtExceptionHandler(restartHandler);
    }
    private void creatFolder() {
        if(FileUtil.createFilePath(Constant.LOG_PATH)){
            Log.e(TAG, "目录创建成功");
        }else {
            Log.e(TAG,"目录创建失败");
        }
        ;
//        FileUtil.createFilePath(Constant.LOG_PATH);
    }


    private void initLogConfig() {
        LogcatHelper.getInstance(this).start();
    }

    private void copyFile() {
        Session session=new Session(this, Constant.SETTING_CONFIG);
        boolean isfisrt=session.getBoolean(Constant.CONFIG_FIRST,false);
        if(!isfisrt){
            session.setBoolean(Constant.CONFIG_FIRST,true);
            FileUtil.copyDbFile(this,"aus.db");
        }
    }


    private Thread.UncaughtExceptionHandler restartHandler = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
            restartApp();
        }
    };

    public void restartApp() {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }


}
